//
//  UIImageExtension.swift
// Kool Car_app
//
//  Copyright © 2018年 yzgt. All rights reserved.
//

import UIKit

var imgView_tap_key: UnsafePointer<String>? = nil

extension UIImageView {
    func addTapGestureRecognizer(numberOfTaps: Int, aBlock: @escaping (Any) -> Void) {
        self.isUserInteractionEnabled = true
        let tempBlock = objc_getAssociatedObject(self, &imgView_tap_key) as? (Any) -> Void
        if tempBlock == nil {
            objc_setAssociatedObject(self, &imgView_tap_key, aBlock, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        }else{
            return
        }
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
        tap.numberOfTapsRequired = numberOfTaps
        self.addGestureRecognizer(tap)
    }
    
    @objc func tapAction(){
        let tempBlock = objc_getAssociatedObject(self, &imgView_tap_key) as? (Any) -> Void
        if tempBlock != nil {
            tempBlock?(self)
        }
    }
}


//用颜色作为图片
extension UIImage {
    static func image(withColor color: UIColor) -> UIImage? {
        let rect = CGRect(x: 0, y: 0, width: 2, height: 3)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        color.setFill()
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    static func tabbarImage(name: String) -> UIImage {
        return UIImage.init(named: name)!.withRenderingMode(.alwaysOriginal)
    }
}

extension UIImage {
    // 修复图片旋转
    func fixOrientation() -> UIImage {
        if self.imageOrientation == .up {
            return self
        }
        
        var transform = CGAffineTransform.identity
        
        switch self.imageOrientation {
        case .down, .downMirrored:
            transform = transform.translatedBy(x: self.size.width, y: self.size.height)
            transform = transform.rotated(by: .pi)
            break
            
        case .left, .leftMirrored:
            transform = transform.translatedBy(x: self.size.width, y: 0)
            transform = transform.rotated(by: .pi / 2)
            break
            
        case .right, .rightMirrored:
            transform = transform.translatedBy(x: 0, y: self.size.height)
            transform = transform.rotated(by: -.pi / 2)
            break
            
        default:
            break
        }
        
        switch self.imageOrientation {
        case .upMirrored, .downMirrored:
            transform = transform.translatedBy(x: self.size.width, y: 0)
            transform = transform.scaledBy(x: -1, y: 1)
            break
            
        case .leftMirrored, .rightMirrored:
            transform = transform.translatedBy(x: self.size.height, y: 0);
            transform = transform.scaledBy(x: -1, y: 1)
            break
            
        default:
            break
        }
        
        let ctx = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0, space: self.cgImage!.colorSpace!, bitmapInfo: self.cgImage!.bitmapInfo.rawValue)
        ctx?.concatenate(transform)
        
        switch self.imageOrientation {
        case .left, .leftMirrored, .right, .rightMirrored:
            ctx?.draw(self.cgImage!, in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(size.height), height: CGFloat(size.width)))
            break
            
        default:
            ctx?.draw(self.cgImage!, in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(size.width), height: CGFloat(size.height)))
            break
        }
        
        let cgimg: CGImage = (ctx?.makeImage())!
        let img = UIImage(cgImage: cgimg)
        
        return img
    }
}
